package dbb

import (
	"bytes"

	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/data/binding"
	"fyne.io/fyne/v2/theme"
	"fyne.io/fyne/v2/widget"
	"github.com/y2h/fyne_poem/widgets"
)

func importSql() widget.ToolbarItem {
	return widget.NewToolbarAction(theme.FileTextIcon(), func() {
		w := widgets.EmptyWin("New database")
		fp := "bak.sql"
		bs := binding.BindString(&fp)
		cc := NewOpenFile1(bs, []string{".sql"}, w)
		txt := widget.NewMultiLineEntry()
		btn := widget.NewButton("Import from File", func() {
			eng := gf2xorm(Me.Db)
			_, err := eng.ImportFile(fp)
			if err != nil {
				txt.SetText(err.Error())
				return
			}
			txt.SetText("Import success")
		})

		buf := bytes.NewBufferString("")
		btn1 := widget.NewButton("Import from Entry", func() {
			buf.Reset()
			buf.WriteString(txt.Text)
			eng := gf2xorm(Me.Db)
			_, err := eng.Import(buf)
			if err != nil {
				txt.SetText(err.Error())
				return
			}
			txt.SetText("Import success")
		})
		top := container.NewVBox(cc, container.NewHBox(btn, btn1))
		w.SetContent(container.NewBorder(top, nil, nil, nil, txt))
		w.Show()
	})
}
